using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.IO;

using Framework;

namespace SchemaMonitor
{
    //Collection Class (Customisable half)
    public partial class CImageMatchedButDifferentList
    {
        #region Filters
        #endregion

        #region Aggregation
        #endregion
        


        #region Cloning
        public CImageMatchedButDifferentList Clone(CDataSrc target) //, int parentId)
        {
            //No Transaction
            if (target is CDataSrcRemote)
                return Clone(target, null); //, parentId);

            //Transaction
            using (IDbConnection cn = target.Local.Connection())
            {
                IDbTransaction tx = cn.BeginTransaction();
                try
                {
                    CImageMatchedButDifferentList clone = Clone(target, tx); //, parentId);
                    tx.Commit();
                    return clone;
                }
                catch
                {
                    tx.Rollback();
                    throw;
                }
            }
        }
        public CImageMatchedButDifferentList Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            CImageMatchedButDifferentList list = new CImageMatchedButDifferentList(this.Count);
            foreach (CImageMatchedButDifferent i in this)
                list.Add(i.Clone(target, txOrNull)); //, parentId));  *Child entities must reference the new parent
            return list;
        }
        #endregion
        
        #region Export to Csv
        //Note: For non-cached classes like this, should normally use CDataSrc.ExportToCsv(SelectWhere_DataSet)

        //Web 
        //Web - Need to add a project reference to System.Web, or comment out these two methods
        public void ExportToCsv(HttpResponse response) { ExportToCsv(response, "ImageMatchedButDifferents.csv"); }
        public void ExportToCsv(HttpResponse response, string fileName)
        {
            CDataSrc.ExportToCsv(response, fileName); //Standard response headers
            StreamWriter sw = new StreamWriter(response.OutputStream);
            ExportToCsv(sw);
            sw.Flush();
            response.End();
        }
        
        //Non-web
        public void ExportToCsv(string filePath)
        {
            StreamWriter sw = new StreamWriter(filePath);
            ExportToCsv(sw);
            sw.Close();
        }

        //Logic
        protected void ExportToCsv(StreamWriter sw)
        {
            string[] headings = new string[] {"MD5", "MatchingMd5", "Score"};
            CDataSrc.ExportToCsv(headings, sw);
            foreach (CImageMatchedButDifferent i in this)
            {
                object[] data = new object[] {i.MD5, i.MatchingMd5, i.Score};
                CDataSrc.ExportToCsv(data, sw);
            }
        }
        #endregion
    }
}
